package com.itheima;

/*准备一个长度是9的日期数组
        使用1970年-2000年之间的随机日期初始化该数组
        按照这些日期的时间进行升序排序
        比如 1988-1-21 12:33:22 就会排在 1978-4-21 19:07:23 前面，因为它的时间更小，虽然日期更大*/
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.Random;

public class Date_sort {
    public static void main(String[] args) throws ParseException {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Calendar calendar = Calendar.getInstance();
        Date[] days = new Date[9];
        Random r = new Random();
        for (int i=0;i<9;i++) {
            int year = r.nextInt(1970,2000);
            int month = r.nextInt(0,12);
            int date= r.nextInt(calendar.getActualMaximum(month));
            int hour = r.nextInt(0,24);
            int minuter = r.nextInt(0,60);
            int second = r.nextInt(0,60);
            calendar.set(year,month,date,hour,minuter,second);
            days[i] =calendar.getTime();
        }
        System.out.println("得到的随机数组:");
        for (Date i:days){
            System.out.println(i);
        }
        long t1;
        long t2;
        Date temp;
        System.out.println("排序后");
        for (int i = 0; i < days.length; i++) {
            for (int j = 0; j < days.length-1-i; j++) {
                t1 = days[j].getTime();
                t2 = days[j+1].getTime();
                if(t1<t2){
                    temp=days[j+1];
                    days[j+1]=days[j];
                    days[j]=temp;
                }
            }
        }
        for(Date d: days){
            System.out.println(d);
        }
    }
}
